package org.huangrui.javase.thread;

import java.util.concurrent.CountDownLatch;

/**
 * 有序执行
 * @Author hr
 * @Create 2025-11-08 13:27
 **/
public class T02OrderedExecution2 {
    public static void main(String[] args) {
        CountDownLatch latch1 = new CountDownLatch(1);
        CountDownLatch latch2 = new CountDownLatch(1);

        Thread t1 = new Thread(() -> {
            System.out.println("Thread 1 is running");
            latch1.countDown(); // 通知t2可以开始
        });

        Thread t2 = new Thread(() -> {
            try {
                latch1.await(); // 等待t1完成，直到latch1的计数器为0为止
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Thread 2 is running");
            latch2.countDown(); // 通知t3可以开始
        });

        Thread t3 = new Thread(() -> {
            try {
                latch2.await(); // 等待t2完成，直到latch2的计数器为0为止
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println("Thread 3 is running");
        });

        t1.start();
        t2.start();
        t3.start();
    }
}
